package com.goldcontrol.cloudweb.controller;

import com.alibaba.fastjson.JSONObject;
import com.goldcontrol.cloudweb.model.JobEntity;
import com.goldcontrol.cloudweb.service.ScheduledService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/wechat/qt")
public class WechatQuartzController {

    private static Logger logger = Logger.getLogger(WechatQuartzController.class);

    @Autowired
    ScheduledService scheduledService;

    private static final Integer ON = 1;
    private static final Integer OFF = 0;

    @ResponseBody
    @RequestMapping(value = "change",method = RequestMethod.GET)
    public String change(@RequestParam Integer onoff,@RequestParam Integer projectId) throws ParseException {
        JSONObject result = new JSONObject();
        try{
            if(onoff == null){
                onoff = 0;
            }
            JobEntity jobEntity = new JobEntity();
            jobEntity.setProjectId(projectId);
            jobEntity.setJobStatus(onoff);
            scheduledService.updateProjectAllJobStatus(projectId,onoff);
            JobEntity job = new JobEntity();

            if(onoff == 0){
                //删除开始和关机定时任务
                job.setJobStatus(OFF);
                scheduledService.removeQuartzJob(projectId,0);
                scheduledService.removeQuartzJob(projectId,1);
            }else{
                job.setJobStatus(ON);
                List<JobEntity> jobEntityList = scheduledService.getJob(projectId);
                for(JobEntity entity : jobEntityList){
                    scheduledService.removeQuartzJob(entity.getProjectId(),entity.getState());
                    scheduledService.addQuartzJob(entity);
                }
            }
            result.put("status",100);
        }catch (Exception e){
            e.printStackTrace();
            result.put("status",200);
        }
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "cmd",method = RequestMethod.POST)
    public String cmd(@RequestBody JSONObject request) {
        JSONObject result = new JSONObject();
        try {
            Integer state = request.getInteger("state");
            Integer projectId = request.getInteger("projectId");
            Integer onoff = request.getInteger("onoff");
            String time = request.getString("time");
            String cronTime = scheduledService.getCronTime(time);

            //根据时间 生成cron表达式
            JobEntity jobEntity = new JobEntity();
            jobEntity.setJobStatus(onoff);
            jobEntity.setProjectId(projectId);
            jobEntity.setCronTime(cronTime);
            jobEntity.setTime(time);
            jobEntity.setState(state);
            scheduledService.updateProjectSingleJobStatus(jobEntity);

            boolean flag = scheduledService.checkJobIfExists(jobEntity);
            try{
                if(flag){
                    scheduledService.addQuartzJob(jobEntity);
                }else{
                    scheduledService.updateQuartzJob(jobEntity);
                }
            }catch (Exception e){
                logger.error("设置定时任务失败",e);
            }
            result.put("status",100);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("cmd error {}",e);
            result.put("status",200);
        }
        return result.toJSONString();
    }

    @ResponseBody
    @RequestMapping(value = "get",method = RequestMethod.GET)
    public String get(@RequestParam Integer projectId){
        List<JobEntity> jobEntityList = scheduledService.getJob(projectId);
        JSONObject result = new JSONObject();
        for(JobEntity jobEntity : jobEntityList){
            if(jobEntity.getState().intValue() == 1){
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("time",jobEntity.getTime());
                result.put("on",jsonObject);
            }else{
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("time",jobEntity.getTime());
                result.put("off",jsonObject);
            }
            result.put("onoff",jobEntity.getJobStatus());
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("data",result);
        return jsonObject.toJSONString();
    }

}
